package com.erp.framework.controller.taglib;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.erp.app.catalog.catgroup.vo.CatalogGroupVO;
import com.erp.app.common.ProjectConstants;
import com.erp.framework.util.user.User;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.util.CommonUtil;

public class CustomCatalogMenuTag extends TagSupport {
    private String menuItemCollection;
	private String realPath;
	public String getRealPath(){
		return this.realPath;
	}
	public void setRealPath(String realPath){
		this.realPath = realPath;
	}

    public String getMenuItemCollection() {
        return menuItemCollection;
    }

    public void setMenuItemCollection(String menuItemCollection) {
        this.menuItemCollection = menuItemCollection;
    }
    
    public int doStartTag() throws JspException {
        JspWriter jspWriter = pageContext.getOut();
        HttpSession session = pageContext.getSession();
       
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        

        Collection rslt = (Collection) pageContext.getAttribute(menuItemCollection);
        if (rslt == null) {
            throw new JspException("Cannot get Collection with property name " + menuItemCollection);
        }

        try {
            jspWriter.println("<table width=\"100%\" height=\"20\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\" bgcolor=\"#FFFFFF\">");

            int i = 0;
            String menuIdStr = "";
            String catIdStr = "";
            Iterator iterator = rslt.iterator();
            while (iterator.hasNext()) {
                CatalogGroupVO menuGroupVO = (CatalogGroupVO) iterator.next();
                String idx = CommonUtil.bigDecimalToStr(menuGroupVO.getL01nodid());
                if (idx.startsWith("0") && idx.length() > 0) {
                    idx = idx.substring(1);
                }
                i++;
                menuIdStr = i>9?String.valueOf(i):"0"+String.valueOf(i);
                catIdStr = CommonUtil.nullToString(menuGroupVO.getL01catid());
                
                jspWriter.println("<tr bgcolor=\"#999999\" align=\"left\">");
                jspWriter.print("\t\t<td class=\"main_menu\" id=\"Menu0"+menuIdStr+"\" name=\"Menu\" height=\"15\"  ");
                jspWriter.print("onMouseOver=\"javascript:menuMouseOverOut(this, 'over');\""
                		+ " onMouseOut=\"javascript:menuMouseOverOut(this, 'out');\" onClick=\"menuMouseClick('Menu0" + menuIdStr + "')\" style=\"cursor:hand\" ");

                jspWriter.print(">");
                String menuGroupDesc = menuGroupVO.getL01dsc();
                String tmpMenuGroupDesc = null;

                if (!CommonUtil.isEmpty(tmpMenuGroupDesc)) {
                    menuGroupDesc = tmpMenuGroupDesc;
                }

                jspWriter.println("<img src=\""+this.realPath+"/images/dhtmlgoodies_folder.gif\" alt=\"Arrow\" width=\"16\" height=\"15\"/><font color=\"#000000\">" + menuGroupDesc + "</font></td>");
                jspWriter.println("</tr>");

                String historyPath = menuGroupDesc;
                String path = "<a href='catalogMainPage.do?catalogLevel="+menuGroupVO.getL01nodid().intValue()+"&menuid=Menu0"+menuIdStr+"&historyPath="+URLEncoder.encode(historyPath)+"'>";
                path += menuGroupDesc;
                path += "</a>";
                ChildList itemList = menuGroupVO.getCatalogGroupChildsList(editingUser.getUserId());
                if (itemList != null && itemList.size() > 0) {
                     CreateSubMenu(jspWriter,menuIdStr,itemList,path, session);
                }

            }

        } catch (Exception e) {
            Logger.getLogger(this.getClass().getName()).error(e.getMessage(), e);
        } finally {
            try {
                jspWriter.println("</table>");
            } catch (Exception e) {
            }
        }

        return SKIP_BODY;
    }

    private void CreateSubMenu(JspWriter jspWriter,String  parentMenuIdStr,ChildList itemList,String parentPath, HttpSession session) throws Exception{
         Iterator iter = itemList.iterator();
         int i=0;
         String menuIdStr = "";
         User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
         while (iter.hasNext()) {
             Object obj = iter.next();
             String path = "";
             i++;
             menuIdStr = i>9?String.valueOf(i):"0"+String.valueOf(i);
             if (obj instanceof CatalogGroupVO) {
                 CatalogGroupVO itemVO = (CatalogGroupVO) obj;
                 ChildList subitemList = itemVO.getCatalogGroupChildsList(editingUser.getUserId());
                 if(subitemList.size() > 0){
                	 String historyPath = parentPath + ProjectConstants.STRING_SPACE + itemVO.getL01dsc();
                     path +=  parentPath + ProjectConstants.STRING_SPACE;
                     path += "<a href='itemMasterSearch.do?action=search&seletStr=&srchArticleNo=&srchDesc=&srchnodid="+itemVO.getL01nodid().intValue()+"&menuid=Menu0"+parentMenuIdStr+menuIdStr+"&historyPath="+URLEncoder.encode(historyPath)+"'>";
                     path += itemVO.getL01dsc();
                     path += "</a>";
                	 jspWriter.println("<tr style=\"display:none;\" bgcolor=\"#CCCCCC\" align=\"left\">");
                     jspWriter.print("\t\t<td class=\"subMenu\" id=\"Menu0"+parentMenuIdStr+menuIdStr+"\" name=\"Menu\" height=\"15\" onMouseOver=\"javascript:menuMouseOverOut(this, 'over');\" onMouseOut=\"javascript:menuMouseOverOut(this, 'out');\" ");
                     jspWriter.print("onClick=\"javascript:menuMouseClick('Menu0"+parentMenuIdStr+menuIdStr+"')\" style=\"cursor:hand\" >");
                     jspWriter.println("<img src=\""+this.realPath+"/images/dhtmlgoodies_folder.gif\" alt=\"Arrow\" width=\"16\" height=\"15\"/><font color=\"#000000\">" + itemVO.getL01dsc() + "</font></td>");
                     jspWriter.println("</tr>");
                 }else{
                     path +=  parentPath + ProjectConstants.STRING_SPACE;
                     path += itemVO.getL01dsc();
                     jspWriter.println("<tr style=\"display:none;\" bgcolor=\"#CCCCCC\" align=\"left\">");
                     jspWriter.print("\t\t<td class=\"subMenu\" id=\"Menu1"+parentMenuIdStr+menuIdStr+"\" name=\"Menu\" height=\"15\" onMouseOver=\"javascript:menuMouseOverOut(this, 'over');\" onMouseOut=\"javascript:menuMouseOverOut(this, 'out');\""
                    		 +" onClick=\"javascript:menuMouseClick('Menu1"+parentMenuIdStr+menuIdStr+"','itemMasterSearch.do?action=search&seletStr=&srchArticleNo=&srchDesc=&srchnodid="+itemVO.getL01nodid().intValue() +"&menuid=Menu1"+parentMenuIdStr+menuIdStr+"&historyPath="+URLEncoder.encode(path)+"');\" style=\"cursor:hand\" >");
                     jspWriter.println("<img src=\""+this.realPath+"/images/dhtmlgoodies_folder.gif\" alt=\"Arrow\" width=\"16\" height=\"15\"/><font color=\"#000000\">" + itemVO.getL01dsc() + "</font></td>");
                     jspWriter.println("</tr>");
                 }

                 if (subitemList != null && subitemList.size() > 0) {
                     	CreateSubMenu2(jspWriter,parentMenuIdStr+menuIdStr,subitemList,path, session);
                 }
            }
         }
    }

    private void CreateSubMenu2(JspWriter jspWriter,String  parentMenuIdStr,ChildList itemList,String parentPath, HttpSession session) throws Exception{
        Iterator iter = itemList.iterator();
        int i=0;
        String menuIdStr = "";
        String catIdStr = "";
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        while (iter.hasNext()) {
            Object obj = iter.next();
            i++;
            menuIdStr = i>9?String.valueOf(i):"0"+String.valueOf(i);
            if (obj instanceof CatalogGroupVO) {
                CatalogGroupVO itemVO = (CatalogGroupVO) obj;
                catIdStr = CommonUtil.nullToString(itemVO.getL01catid());
                String path =  parentPath + ProjectConstants.STRING_SPACE;
                path += itemVO.getL01dsc();
                jspWriter.println("<tr style=\"display:none;\" bgcolor=\"#CCCCCC\" align=\"left\">");
                jspWriter.print("\t\t<td class=\"subMenu2\" id=\"Menu1"+parentMenuIdStr+menuIdStr+"\" name=\"Menu\" height=\"15\" onMouseOver=\"javascript:menuMouseOverOut(this, 'over');\" onMouseOut=\"javascript:menuMouseOverOut(this, 'out');\" onClick=\"javascript:doSubmit('searchExtra','"+catIdStr+"');\" style=\"cursor:hand\" >" );
                jspWriter.println("<img src=\""+this.realPath+"/images/dhtmlgoodies_folder.gif\" alt=\"Arrow\" width=\"16\" height=\"15\"/><a href=\"catalogLogin.do?pageEvent='searchExtra'&pageEventSrc='"+itemVO.getL01nodid()+"'\"><font color=\"#000000\">" + itemVO.getL01dsc() + "</font></a></td>");
                jspWriter.println("</tr>");
           }
        }
   }

	


}
